class TreeNode:
    def __init__(self):
        self.val = 0
        self.sons = []

    def addSon(self, son):
        self.sons.append(son)

    def setVal(self, val):
        self.val = val


n, m = map(int, input().split())
nodes = [TreeNode() for _ in range(n + 1)]
for i in range(1, n + 1):
    si, ki = map(int, input().split())
    nodes[si].addSon(nodes[i])
    nodes[i].setVal(ki)

dp = [[[0 for _ in range(2)] for _ in range(m)] for _ in range(n + 1)]

max_val = 0


def dfs(node: TreeNode):
    global max_val
    for son in node.sons:
        dp
